package com.shuosen.gmall.list.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.shuosen.gmall.bean.*;
import com.shuosen.gmall.service.ListService;
import com.shuosen.gmall.service.ManageService;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.lang.ref.ReferenceQueue;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Controller
public class ListController {

    @Reference
    ListService  listService  ;

    @Reference
    ManageService  manageService  ;

    @RequestMapping("/list.html")
//    @ResponseBody
    public  String getList(SkuLsParams skuLsParams, HttpServletRequest request){
        //设置pagesize大小
        skuLsParams.setPageSize(2);

        SkuLsResult skuLsResult = listService.search(skuLsParams);
        List<SkuLsInfo> skuLsInfoList = skuLsResult.getSkuLsInfoList();

        // 平台属性平台属性值集合
        List<String> attrValueIdList = skuLsResult.getAttrValueIdList();
        List<BaseAttrInfo>  baseAttrInfoList = manageService.getAttrList(attrValueIdList);
        //平台属性值集合打印
        request.setAttribute("baseAttrInfoList",baseAttrInfoList);
        request.setAttribute("skuLsInfoList",skuLsInfoList);

        System.out.println("skuLsInfoList"+skuLsInfoList.toString());
        System.out.println("baseAttrInfoList"+baseAttrInfoList.toString());
        //baseAttrInfo List 显示到页面的数据  包含了平台属性，平台属性值的名称，
        // 如果urlParams中的valueId与BaseAttrValue.id相等 则将baseAttrInfoList中的这个属性中对象移除
        //声明一个字符串来保存条件拼接的参数
        //使用迭代器循环集合 ，才能将数据删除
        //itco
        //声明一个字符串来保存条件拼接的url
        //声明一个存储面包屑的集合
        ArrayList<BaseAttrValue>  baseAttrValueArrayList = new ArrayList<>();
        for(Iterator<BaseAttrInfo> iterator = baseAttrInfoList.iterator();iterator.hasNext();){
            BaseAttrInfo baseAttrInfo = iterator.next();
            List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
            for (BaseAttrValue baseAttrValue : attrValueList) {
                if(skuLsParams.getValueId()!=null && skuLsParams.getValueId().length>0){
                    for (String valueId : skuLsParams.getValueId()) {
                         //选中的属性值和查询结果的属性值
                        if(valueId.equals(baseAttrValue.getId())){
                            iterator.remove();
                            //创建一个平台属性值对象
                            BaseAttrValue baseAttrValueed = new BaseAttrValue();
                            baseAttrValueed.setValueName(baseAttrInfo.getAttrName()+":"+baseAttrValue.getValueName());
                            //重构urlParam条件
                            //此时urlParams应该是最新的urlparam，应该把urlparam保存
                            String urlParams = makeUrlParam(skuLsParams, valueId);
                            //保存最新的urlParam参数
                            baseAttrValueed.setUrlParam(urlParams);
                            //将构造好的平台属性值对象放入集合
                            baseAttrValueArrayList.add(baseAttrValueed);
                        }
                    }

                }
            }
        }
        //保存关键字
        request.setAttribute("keyword",skuLsParams.getKeyword());
        //保存到作用域
        request.setAttribute("baseAttrValueArrayList",baseAttrValueArrayList);

        String urlParam = makeUrlParam(skuLsParams);
        request.setAttribute("urlParam",urlParam);

        request.setAttribute("totalPages",skuLsResult.getTotalPages());

        request.setAttribute("pageNo",skuLsParams.getPageNo());


        return  "list" ;
    }

    private String makeUrlParam(SkuLsParams skuLsParams,String... excludeValuedIds) {
        String urlParam = "";
        //总的条件
        if(skuLsParams.getKeyword()!=null && skuLsParams.getKeyword().length()>0){
            urlParam+="keyword="+skuLsParams.getKeyword();

        }
        //拼接三级分类id
        if(skuLsParams.getCatalog3Id()!=null && skuLsParams.getCatalog3Id().length()>0){
            //什么时候追加&
            if(urlParam.length()>0){
                urlParam+="&" ;
            }
            urlParam +="catalog3Id="+skuLsParams.getCatalog3Id();
        }

        //拼接平台属性值id
        if(skuLsParams.getValueId()!=null && skuLsParams.getValueId().length>0){
            for (String valueId : skuLsParams.getValueId()) {
                if(excludeValuedIds!=null && excludeValuedIds.length>0){
                    //获取valueId
                    String excludeValuedId = excludeValuedIds[0];
                    if(valueId.equals(excludeValuedId)){
                        //如果valueId相等，则不应该拼接参数
                     //break;
                        continue;
                    }
                }
                if(urlParam.length()>0){
                    urlParam+="&" ;
                }
                urlParam+="valueId="+valueId ;
            }
        }
        return urlParam;
    }
}

